﻿using Entity;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System.Linq.Expressions;

namespace DAL.Interface
{
    public interface IBaseRepository<TEntity> where TEntity : class 
    {
        /// <summary>
        /// 添加单条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Add(TEntity entity);

        /// <summary>
        /// 添加单条数据（异步）
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Task<int> AddAsync(TEntity entity);
        /// <summary>
        /// 批量添加——集合
        /// </summary>
        /// <param name="entityList"></param>
        /// <returns></returns>
        public int AddRange(List<TEntity> entityList);

        /// <summary>
        /// 批量添加——集合（异步）
        /// </summary>
        /// <param name="entityList"></param>
        /// <returns></returns>
        public Task<int> AddRangeAsync(List<TEntity> entityList);
        /// <summary>
        /// 查询出一条数据
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public TEntity FirstOrDefault(Expression<Func<TEntity,bool>>? action);
        /// <summary>
        /// 查询出一条数据(异步)
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>>? action =null);
        /// <summary>
        /// 查询出Queryable数据
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public IQueryable Queryable(Expression<Func<TEntity, bool>> action);

        /// <summary>
        /// 查出集合(异步)
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public  Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>>? predicate = null,CancellationToken cancellationToken = default);
        /// <summary>
        /// 查出集合
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public List<TEntity> GetList(Expression<Func<TEntity, bool>>? predicate = null);


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(TEntity entity);
        /// <summary>
        /// 修改（异步）
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int UpdateRange(TEntity entity);

        /// <summary>
        /// 提交事务(异步)
        /// </summary>
        /// <param name="asyncAction"></param>
        /// <returns></returns>
        /// <exception cref="MyCustomException"></exception>
        public   Task SaveChangesWithTransactionAsync(Func<Task> asyncAction);
        /// <summary>
        /// 提交事务
        /// </summary>
        /// <param name="action"></param>
        /// <exception cref="MyCustomException"></exception>
        public void SaveChangesWithTransaction(Action action);
    }
}
